<?php
!defined ('P_W') && exit('Forbidden');
class PW_HouseImageType {
	var $topTypes;
	var $secendTypes;
	var $_imageTypeDB;
	var $_haveInit=false;
	
	function __construct() {
		//$this->_init();
	}
	function PW_HouseImageType() {
		$this->__construct();
	}
	
	function getType($id) {
		$imageTypeDB = $this->_getImageTypeDB();
		return $imageTypeDB->get($id);
	}
	
	function _deleteType($id) {
		$imageTypeDB = $this->_getImageTypeDB();
		$imageTypeDB->delete($id);
		$imageService = House::loadClass('houseimage'); /* @var $imageService PW_HouseImage */
		$imageService->deleteImagesByTypeId($id);
	}
	function deleteType($id) {
		$secendTypes = $this->getSecendTypesById($id);
		if ($secendTypes) {
			foreach ($secendTypes as $key=>$value) {
				$this->_deleteType($key);
			}
		}
		$this->_deleteType($id);
		$this->cacheTypes();
	}
	function addType($name,$sub,$position) {
		$imageTypeDB = $this->_getImageTypeDB();
		$check= $imageTypeDB->getByName($name);
		if ($check) return false;
		$temp = $imageTypeDB->insert(array('name'=>$name,'sub'=>$sub,'position'=>$position));
		if ($temp) {
			$this->cacheTypes();
			return $temp;
		}
		return false;
	}
	function updateImageTypes($imageTypes) {
		$imageTypeDB = $this->_getImageTypeDB();
		$allTypes = $imageTypeDB->getAll();
		foreach ($imageTypes as $key=>$imagetype) {
			$imagetype['ifuse'] = (bool) $imagetype['ifuse'];
			if ($allTypes[$imagetype['id']]['ifsystem']) {
				$imageTypeDB->update($imagetype['id'],array('position'=>$imagetype['position'],'ifuse'=>$imagetype['ifuse']));
			} else {
				!$imagetype['name'] && Showmsg('name must be definded');
				$imageTypeDB->update($imagetype['id'],array('name'=>$imagetype['name'],'position'=>$imagetype['position'],'ifuse'=>$imagetype['ifuse']));
			}
		}
		$this->cacheTypes();
	}
	
	function cacheTypes() {
		$imageTypeDB = House::loadDB('house_imagetype');
		$temp = $imageTypeDB->getAll();
		
		$topTypes = $secendTypes = array();
		foreach ($temp as $key => $imageType) {
			if (!$imageType['sub']) {
				$topTypes[$key] = $imageType;
			} else {	
				$secendTypes[$imageType['sub']][$key] = $imageType;
			}
		}
		$cacheFile = $this->_getCacheFile();
		writeover($cacheFile,"<?php\r\n\$topTypes=".pw_var_export($topTypes).";\r\n\$secendTypes=".pw_var_export($secendTypes).";\r\n?>");
	}
	
	function &getTopTypes() {
		if (!$this->_haveInit) $this->_initCache();
		return $this->topTypes;
	}
	function &getSecendTypes() {
		if (!$this->_haveInit) $this->_initCache();
		return $this->secendTypes;
	}
	function &getSecendTypesById($id) {
		if (!$this->_haveInit) $this->_initCache();
		$id = (int)$id;
		if (isset($this->secendTypes[$id])) return $this->secendTypes[$id];
		return array();
	}
	function _initCache() {
		$cacheFile = $this->_getCacheFile();
		if (!is_file($cacheFile)) {
			$this->cacheTypes();
		}
		require Pcv($cacheFile);
		$this->topTypes =& $topTypes;
		$this->secendTypes =& $secendTypes;
		$this->_haveInit = true;
	}
	function _getImageTypeDB() {
		return House::loadDB('house_imagetype');
	}
	function _getCacheFile() {
		return D_P.'data/bbscache/house_imagetype.php';
	}
}
?>